package com.yehaikeji.workflow.service.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.yehaikeji.workflow.bean.Model;
import com.yehaikeji.workflow.bean.Receive;
import com.yehaikeji.workflow.bean.SmsOperation;
import com.yehaikeji.workflow.mapper.ModelMapper;
import com.yehaikeji.workflow.mapper.SmsOperationMapper;
import com.yehaikeji.workflow.service.SmsOperationService;
import com.yehaikeji.workflow.util.MessageSend;

@Service
public class SmsOperationServiceImpl implements SmsOperationService {

	@Autowired
	private SmsOperationMapper smsOperationMapper;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Transactional
	@Override
	public int addSmsOperation(SmsOperation smsOperation) {
		smsOperation.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		smsOperation.setCreateDate(new Date());
		smsOperation.setStatus("0");
		
		Model model = modelMapper.selectByPrimaryKey(smsOperation.getModelId());
		if(null == model){
			throw new RuntimeException("短信模板ID不正确");
		}
		
		if(!model.getContent().equals(smsOperation.getContent().trim())){
			model.setContent(smsOperation.getContent().trim());
			modelMapper.updateByPrimaryKeySelective(model);
		}
		
		Map<String,String> resultMap = MessageSend.sendMessage(smsOperation.getPhone(), smsOperation.getContent().trim());
		String status = resultMap.get("status");
		if("0".equals(status)){//请求失败
			smsOperation.setStatus("0");
			smsOperation.setRemark(resultMap.get("msg"));
		}else{
			smsOperation.setStatus("1");
			smsOperation.setSendId(resultMap.get("msg"));
			smsOperation.setRemark("短信发送成功！");
		}
		return smsOperationMapper.insertSelective(smsOperation);
	}

	@Override
	public int addReceiveSms(Receive receive) {
		SmsOperation smsOperation = new SmsOperation();
		smsOperation.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		smsOperation.setCreateDate(new Date());
		smsOperation.setStatus("1");
		smsOperation.setType("2");
		smsOperation.setContent(receive.getContent());
		smsOperation.setPhone(receive.getAddress());
		
		List<String> thingIdList = smsOperationMapper.selectLastThingIdByPhone(receive.getAddress());
		if(CollectionUtils.isEmpty(thingIdList)){
			smsOperation.setRemark("短信上行接受未知消息，此消息不作任何业务处理");
		}else{
			smsOperation.setThingId(thingIdList.get(0));
			smsOperation.setRemark("短信上行接受正确到消息");
		}
		return smsOperationMapper.insertSelective(smsOperation);
	}

	@Override
	public List<SmsOperation> getChatDetailByPhone(String phone,String thingId) {
		Map<String,String>  map = new HashMap<>();
		map.put("phone", phone);
		map.put("thingId", thingId);
		return smsOperationMapper.selectByPhone(map);
	}

	@Override
	public int modifyStatusBySendId(String sendId,String status,String remark) {
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("sendId", sendId);
		paramMap.put("status", status);
		paramMap.put("remark", remark);
		return smsOperationMapper.updateStatusBySendId(paramMap);
	}

}
